import { createRouter, createWebHistory } from 'vue-router';
import store from '../store';

// 导入主页和通用页面
import HomeView from '../views/main/HomeView.vue';
import AboutView from '../views/main/AboutView.vue';
import NotFoundView from '../views/main/NotFoundView.vue';

// 导入工具页面
import TextToolsView from '../views/tools/TextToolsView.vue';
import ImageToolsView from '../views/tools/ImageToolsView.vue';
import ConvertersView from '../views/tools/ConvertersView.vue';
import EfficiencyToolsView from '../views/tools/EfficiencyToolsView.vue';
import DataToolsView from '../views/tools/DataToolsView.vue';
import DesignToolsView from '../views/tools/DesignToolsView.vue';

// 导入用户相关页面
import LoginView from '../views/user/LoginView.vue';
import RegisterView from '../views/user/RegisterView.vue';
import ProfileView from '../views/user/ProfileView.vue';
import MembershipView from '../views/user/MembershipView.vue';

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      title: '首页 - 柯基工具箱'
    }
  },
  {
    path: '/text-tools',
    name: 'text-tools',
    component: TextToolsView,
    meta: {
      title: '文本工具 - 柯基工具箱'
    }
  },
  {
    path: '/image-tools',
    name: 'image-tools',
    component: ImageToolsView,
    meta: {
      title: '图像工具 - 柯基工具箱'
    }
  },
  {
    path: '/converters',
    name: 'converters',
    component: ConvertersView,
    meta: {
      title: '格式转换 - 柯基工具箱'
    }
  },
  {
    path: '/efficiency-tools',
    name: 'efficiency-tools',
    component: EfficiencyToolsView,
    meta: {
      title: '效率工具 - 柯基工具箱'
    }
  },
  {
    path: '/data-tools',
    name: 'data-tools',
    component: DataToolsView,
    meta: {
      title: '数据工具 - 柯基工具箱'
    }
  },
  {
    path: '/design-tools',
    name: 'design-tools',
    component: DesignToolsView,
    meta: {
      title: '设计工具 - 柯基工具箱'
    }
  },
  {
    path: '/profile',
    name: 'profile',
    component: ProfileView,
    meta: { requiresAuth: true }
  },
  {
    path: '/membership',
    name: 'membership',
    component: MembershipView,
    meta: {
      title: '会员计划 - 柯基工具箱'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: {
      title: '登录 - 办公工具箱'
    }
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta: {
      title: '注册 - 办公工具箱'
    }
  },
  {
    path: '/about',
    name: 'about',
    component: AboutView,
    meta: {
      title: '关于我们 - 柯基工具箱'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFoundView,
    meta: {
      title: '页面未找到 - 柯基工具箱'
    }
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 页面切换时滚动到顶部
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

// 路由前置守卫，动态修改页面标题
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 检查路由是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 如果需要登录权限但用户未登录，则重定向到登录页
    if (!store.state.isLoggedIn) {
      // 保存要访问的页面，登录后可以重定向回来
      sessionStorage.setItem('redirectPath', to.fullPath);
      
      // 使用ElMessage需要在Vue组件中，这里我们通过sessionStorage传递消息
      sessionStorage.setItem('loginMessage', '请先登录后再访问此页面');
      
      next({ 
        path: '/login',
        query: { redirect: to.fullPath }
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router; 